package com.chen.recruit.controller;

import java.util.Arrays;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.recruit.common.annotation.Log;
import com.chen.recruit.common.api.CommonResult;
import com.chen.recruit.common.constants.MessageConstants;
import com.chen.recruit.common.enums.BusinessType;
import com.chen.recruit.common.utils.ExcelUtils;
import com.chen.recruit.common.valid.AddGroup;
import com.chen.recruit.common.valid.UpdateGroup;
import com.chen.recruit.domain.Classroom;
import com.chen.recruit.domain.model.PageQuery;
import com.chen.recruit.domain.vo.ClassroomVo;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.chen.recruit.service.ClassroomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "教室管理 ClassroomController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/recruit/classroom")
public class ClassroomController {

    private final ClassroomService classroomService;

    @ApiOperation("新增教室")
    @SaCheckPermission("recruit:classroom:add")
    @Log(module = "招聘管理-教室管理", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@Validated(AddGroup.class) @RequestBody ClassroomVo classroomVo) {
        return classroomService.save(BeanUtil.toBean(classroomVo, Classroom.class)) ? CommonResult.success() : CommonResult.fail();
    }

    @ApiOperation("删除教室")
    @SaCheckPermission("recruit:classroom:delete")
    @Log(module = "招聘管理-教室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult delete(@NotEmpty(message = "主键不能为空") @PathVariable Integer[] ids) {
        long count = classroomService.count(
                Wrappers.<Classroom>lambdaQuery().in(Classroom::getId, ids)
                        .eq(Classroom::getStatus, "占用")
        );
        if (count > 0) return CommonResult.fail(MessageConstants.DELETE_CLASSROOM_FAIL);
        return classroomService.removeByIds(Arrays.asList(ids)) ? CommonResult.success() : CommonResult.fail();
    }

    @ApiOperation("更新教室")
    @SaCheckPermission("recruit:classroom:update")
    @Log(module = "招聘管理-教室管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult update(@Validated(UpdateGroup.class) @RequestBody ClassroomVo classroomVo) {
        return null;
    }

    @ApiOperation("获取教室详细信息")
    @SaCheckPermission("recruit:classroom:get")
    @GetMapping("/{id}")
    public CommonResult getInfo(@NotNull(message = "教室id不能为空") @PathVariable("id") Integer id) {
        return CommonResult.success(classroomService.getById(id));
    }

    @ApiOperation("查询教室列表")
    @SaCheckPermission("recruit:classroom:list")
    @GetMapping("/list")
    public CommonResult list(ClassroomVo classroomVo, PageQuery pageQuery) {
        return CommonResult.success(classroomService.queryPageList(classroomVo, pageQuery));
    }

    @ApiOperation("导出教室列表")
    @SaCheckPermission("recruit:classroom:export")
    @Log(module = "招聘管理-教室管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        ExcelUtils.exportExcel(response, "教室列表", Classroom.class, classroomService.list());
    }

}
